package com.hnevc.asyncokhttp3demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FoodDetailActivity extends AppCompatActivity {

    Food mFood;

    TextView mTvTitle;
    TextView mTvFoodName;
    TextView mTvFoodPrice;
    TextView mTvFoodDesc;
    ImageView mIvFoodPic;
    Button mBtnChooseIt;
    Button mBtnBack;
    ImageButton mBtnAddFood;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);

        int foodId = getIntent().getIntExtra("foodId",1);
        initView();
        getFoodFromNet(foodId);
        initEvent();
    }

    private void initEvent() {
        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FoodDetailActivity.this.finish();
            }
        });
        mBtnAddFood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();

                intent.setClass(FoodDetailActivity.this,AddFoodActivity.class);
                startActivity(intent);
            }
        });

    }

    private void initView() {
        mTvTitle  = (TextView) findViewById(R.id.id_tv_title);
        mTvFoodName = (TextView) findViewById(R.id.id_tv_food_name);
        mTvFoodPrice = (TextView) findViewById(R.id.id_tv_food_price);
        mTvFoodDesc = (TextView) findViewById(R.id.id_tv_food_desc);
        mIvFoodPic = (ImageView) findViewById(R.id.id_iv_food_pic);
        mBtnChooseIt = (Button) findViewById(R.id.id_btn_choose_it);
        mBtnBack = (Button) findViewById(R.id.id_btn_back);

        mBtnAddFood = (ImageButton) findViewById(R.id.id_ib_add);
    }

    private void getFoodFromNet(int foodId) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://"+getResources().getString(R.string.ServerIp)+":8080/RestaurantServer/Food/ShowFood/"+foodId)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(FoodDetailActivity.this, "访问失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                Gson gson = new Gson();
                mFood = gson.fromJson(data, Food.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //更新界面显示
                        mTvTitle.setText(mFood.getFoodName());
                        mTvFoodName.setText(mFood.getFoodName());
                        mTvFoodPrice.setText(mFood.getFoodPrice()+"");
                        mTvFoodDesc.setText(mFood.getFoodDesc());
                        String imgUrl = "http://"
                                + getResources().getString(R.string.ServerIp)
                                + ":8080/RestaurantServer/uploadFoodImage/" + mFood.getFoodPic();
                        Picasso.with(FoodDetailActivity.this).load(imgUrl).into(mIvFoodPic);
                        mBtnChooseIt = (Button) findViewById(R.id.id_btn_choose_it);
                        mBtnBack = (Button) findViewById(R.id.id_btn_back);
                    }
                });
            }
        });
    }
}
